package com.zhugang.week13.tree;

import java.util.List;

/**
 * @program algorithms
 * @description: maxDepth
 * @author: chanzhugang
 * @create: 2022/12/29 10:07
 */
public class maxDepth3 {

    /**
     * 559 N叉树的最大深度
     * https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/
     *
     * @param root
     * @return
     */
    public int maxDepth(Node root) {
        /**
         * 和二叉树的最大深度类似
         */
        if (root == null) {
            return 0;
        }
        int childMaxDepth = 0;
        for (int i = 0; i < root.children.size(); i++) {
            // 递归子节点
            int depth = maxDepth(root.children.get(i));
            // 更新最大值
            childMaxDepth = Math.max(childMaxDepth, depth);
        }
        return childMaxDepth + 1;
    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }
}